<?php
namespace Api\Model;
use Think\Model;
class UserModel extends CommonModel{
	/**
	 * 生成用户ID
	 */
	public function generate_user_id(){
		//随机生成9位数的用户ID
		$chars   = str_repeat('012356789',3);
		$chars   = str_shuffle($chars);
		$user_id = substr($chars,0,9);
		//第一位为0的处理
		if(substr($user_id, 0,1) == 0){
			$rand_id = str_shuffle(str_replace('0', '', $user_id));
			$user_id = substr($rand_id, 0,1).substr($user_id, 1);
		}
		//如果存在，再次生成ID
		if($this->is_exists_id($user_id)){
			return $this->generate_user_id();
		}
		return $user_id;
	}
	/**
     * 判断用户手机号是否存在
     * @param  [type]  $mobile 手机号
     * @return boolean         [description]
     */
    public function mobile_exists($mobile){
    	$where['mobile'] = $mobile;
    	$count = D('User')->where($where)->count();
    	return $count > 0;
    }
    /**
     * 判断用户是否存在
     * @param  [type]  $user_id 用户ID
     * @return boolean          [description]
     */
    public function is_exists_id($user_id){
    	$where['user_id'] = $user_id;
    	$count = $this->where($where)->count();
    	return $count > 0;
    }
    /**
     * 判断用户是否设置
     * @param  [type]  $user_id 用户ID
     */
    public function is_set_password($user_id){
		$where['user_id']  = $user_id;
		$where['password'] = array('neq', '');
    	$count = $this->where($where)->count();
    	return $count > 0;
    }
    /**
     * 获取手机号用户ID
     * @param  string $mobile 手机号
     */
    public function get_mobile_user_id($mobile=''){
    	$where['mobile'] = $mobile;
    	$user_id = D('User')->where($where)->getField('user_id');
    	return $user_id;
    }
    public function user_info($user_id){
    	$where['user_id'] = $user_id;
    	$data = D('User')->where($where)->find();

		$data['user_name']     = $this->get_user_name($user_id);
		$data['province_name'] = D('Area')->id2names($data['province_id']);
		$data['city_name']     = D('Area')->id2names($data['city_id']);
		$data['area_name']     = D('Area')->id2names($data['area_id']);
		$data['head_url']      = $this->get_user_head_url($user_id);
		$data['set_password']  = $data['password'] != '';

		//性别处理
		$data['sex_desc']  = get_type_desc($data['sex'], 'sex_desc');
		//用户绑定信息
		$data['bind_info'] = $this->get_bind_sso_info($user_id);

		return $data;
    }
    /**
     * 律师用户信息
     * @param  [type] $user_id 用户ID
     */
    public function lawyer_info($user_id){
    	$where['user_id'] = $user_id;
    	$data = D('UserLawyer')->where($where)->find();

    	if(!$data){
    		return array();
    	}

		$data['user_name']          = $this->get_user_name($user_id);
		$data['province_name']      = D('Area')->id2names($data['province_id']);
		$data['city_name']          = D('Area')->id2names($data['city_id']);
		$data['area_name']          = D('Area')->id2names($data['area_id']);
		$data['head_url']           = $this->get_lawyer_head_url($user_id);
		$data['set_password']       = $data['password'] != '';
		$data['identification_url'] = D('File')->get_path($data['identification_id']);//证件照片URL
		$data['skilled_name']       = D('Category')->get_name($data['skilled_id']);//擅长领域
		$data['audit_status_desc']  = get_type_desc($data['audit_status']);

		return $data;
    }
    /**
     * 获取用户积分
     * @param  string $user_id 用户ID
     */
    public function get_user_score($user_id = ''){
    	$where['user_id'] = $user_id;
    	$score = D('User')->where($where)->getField('score');

    	return $score;
    }
    /**
     * 获取用户名称
     * @param  string $user_id 用户ID
     */
    public function get_user_name($user_id = ''){
    	$user_name = '';
    	
    	$where['user_id'] = $user_id;
    	$info = D('User')->where($where)->find();
    	if($info){
    		$user_name = $info['nickname'] ? $info['nickname'] : substr_replace($data['mobile'], '****', 3, 4);
    	}
    	return $user_name;
    }
    /**
     * 获取用户绑定第三方信息
     * @param  [type] $user_id [description]
     * @return [type]          [description]
     */
    public function get_bind_sso_info($user_id){
    	$data = array();
    	$sso_bind = array('weibo', 'weixin', 'qq');
    	foreach ($sso_bind as $type) {
			$where['user_id']  = $user_id;
			$where['sso_type'] = $type;
    		$info = D('UserSso')->where($where)->find();
    		$data['is_bind_' . $type] = $info['open_id'] != '';
    		$data['nickname_' . $type] = $info['nickname'] ? $info['nickname'] : '';
    	}
    	return $data;
    }
    /**
	 * 获取用户头像URL
	 * @param  [type] $user_id 用户ID
	 */
	public function get_user_head_url($user_id){
		$where['user_id'] = $user_id;
		$info = D('User')->field('head_id,sex')->where($where)->find();
		$head_id = $info['head_id'];
		if(!$head_id){
			switch ($info['sex']) {
				//男头像
				case '1':
					$default_head = 'head-man-default.png';
					break;
				//女头像
				case '2':
					$default_head = 'head-woman-default.png';
					break;
				//默认头
				default:
					$default_head = 'head-man-default.png';
					break;
			}
		}
		$head_url = D('File')->get_path($head_id ? $head_id : $default_head);
		return $head_url;
	}
	
	/**
	 * 判断是否收藏
	 * @param  [type]  $user_id 用户ID
	 * @param  [type]  $fav_id  收藏ID
	 * @param  string  $type    收藏类型
	 */
	public function is_fav($user_id, $fav_id, $type = 'article'){
		$where['user_id'] = $user_id;
		$where['fav_id']  = $fav_id;
		$where['type']    = $type;

		$count = D('UserFav')->where($where)->count();

		return $count > 0;
	}
	/**
	 * 设置收藏
	 * @param [type] $user_id 用户ID
	 * @param [type] $fav_id  收藏ID
	 * @param string $type    收藏类型
	 */
	public function set_fav($user_id, $fav_id, $type = 'article'){

		$is_fav = $this->is_fav($user_id, $fav_id, $type);

		$where['user_id'] = $user_id;
		$where['fav_id']  = $fav_id;
		$where['type']    = $type;

		if($is_fav){
			D('UserFav')->where($where)->delete();
			if($type == 'article'){
				D('Article')->update_num($fav_id, 'fav_num', 'minus');
			}
			return 2;//取消收藏
		}else{
			$data = $where;
			$data['create_time'] = time();
			D('UserFav')->add($data);
			if($type == 'article'){
				D('Article')->update_num($fav_id, 'fav_num', 'add');
			}
			return 1;//添加收藏
		}
	}
	/**
	 * 获取用户收藏
	 * @param  [type] $user_id [description]
	 * @param  string $type    [description]
	 * @return [type]          [description]
	 */
	public function get_fav($user_id, $type = 'article', $page=1, $pagesize=10){
		$where['user_id'] = $user_id;
		$where['type']    = $type;

		$data['count'] = D('UserFav')->where($where)->count();
		$list  = D('UserFav')->where($where)->order('create_time desc')->select();
		foreach ($list as $rs) {
			$rs['fav_date'] = date('Y-m-d', $rs['create_time']);
			$rs['fav_dtime'] = date('Y-m-d H:i:s', $rs['create_time']);
			$new_list[] = $rs;
		}
		$data['list'] = $new_list;
		
		return $data;
	}
	//获取用户城市ID
	public function get_user_city_id($user_id){
		$where['user_id'] = $user_id;
		$city_id = $this->where($where)->getField('city_id');
		return $city_id;
	}
    /**
	 * 生成用户TOKEN
	 * @param  string  $user_id 用户ID
	 * @param  integer $type    登录类型 1Android 2iOS
	 * @param  integer $imei    设备唯一标识
	 * @param  integer $device  设备信息 如iPhone 5s
	 */
	public function make_token($user_id = ''){
		$token_model = D('UserToken');
		$imei        = $_SERVER['HTTP_APP_IMEI'];
		$app_type    = $_SERVER['HTTP_APP_TYPE'];
		$device      = $_SERVER['HTTP_APP_DEVICE'];

		$app_type = $app_type ? $app_type : 'android';
		//踢出其它设备登录用户
		$where['user_id']  = $user_id;
		$where['app_type'] = array('neq','web');//处理APP登录
		//处理WEB登录
		if($app_type == 'web'){
			$where['app_type'] = 'web';
		}
		$token_model->where($where)->save(array('create_time'=>0));

		unset($where);
		$where['user_id']  = $user_id;
		$where['app_type'] = $app_type;
		$old_token = $token_model->where($where)->getField('token');

		$token = md5($user_id . $app_type .uniqid());

		$data['app_type']    = $app_type;
		$data['user_id']     = $user_id;
		$data['token']       = $token;
		$data['ip']          = get_client_ip();
		$data['create_time'] = time();
		//设备信息
		$data['device'] = $device ? $device : '';
		//设备唯一标识
		$data['imei']   = $imei ? $imei : '';

		if($old_token){
			$where['token'] = $old_token;
			$token_model->where($where)->save($data);
		}else{
			$token_model->add($data);
		}
		return $token;
	}
	/**
	 * 获取用户现有TOKEN
	 * @param  [type] $user_id   用户ID
	 * @return [type]            TOKEN
	 */
	public function get_token($user_id){
		$app_type  = $_SERVER['HTTP_APP_TYPE'];

		$app_type = $app_type ? $app_type : 'android';

		$where['user_id']   = $user_id;
		$where['app_type']  = $app_type;
		$token = D('UserToken')->where($where)->getField('token');

		return $token ? $token : '';
	}
	/**
	 * 获取TOKEN的用户ID
	 * @param  string $token [description]
	 * @return [type]        [description]
	 */
	public function get_token_user_id($token=''){
		if(!$token){
			$get_token = $_REQUEST['token'];
			$app_token = $_SERVER['HTTP_APP_TOKEN'];
			$token     = $get_token ? $get_token : $app_token;
		}
		if(!$token){
			return 0;
		}
		$where['token'] = $token;
		$user_id = D('UserToken')->where($where)->getField('user_id');
		return $user_id ? $user_id : 0;
	}

	/**
	 * 获取第三方用户ID
	 * @param  [type] $open_id  [description]
	 * @param  string $sso_type [description]
	 * @return [type]           [description]
	 */
	public function get_sso_user_id($open_id, $sso_type='weixin'){
		$where['sso_type'] = $sso_type;
		$where['open_id']  = $open_id;
		$user_id = D('UserSso')->where($where)->getField('user_id');
		return $user_id ? $user_id : '';
	}

	/**
     * 是否绑定第三方账号
     * @param  [type]  $user_id  用户ID
     * @param  string  $sso_type 第三方类型，weixin微信
     */
    public function is_bind($user_id,$sso_type='weixin'){
		$where['user_id']  = $user_id;
		$where['sso_type'] = $sso_type;
    	$count = D('UserSso')->where($where)->count();
    	return $count > 0;
    }
	
}